package com.common;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import util.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by sola on 2017/9/26.
 */
@RestController
@RequestMapping("/repositoryController")
public class RepositoryCreateController {
    @RequestMapping(value = "/createrepository",method = RequestMethod.POST)
    public void ListUser(String name) {
        //ModelAndView modelAndView = new ModelAndView("/index");
        Logger.debug("StartCreaterepository:"+ name + "Repositority.java");
        SimpleDateFormat sdfDays = new SimpleDateFormat("yyyy/MM/dd");
        String date = sdfDays.format(new Date());
        String repositroy = "package com.TODO.repostiory;\n" +
                "\n" +
                "import com.TODO.domain." + name + ";\n" +
                "import com.repository.ReadOnlyPagingAndSortingRepository;\n" +
                "import org.springframework.data.rest.core.annotation.RepositoryRestResource;\n" +
                "\n" +
                "\n" +
                "/**\n" +
                " * Created by sola on "+date+"\n" +
                " */\n" +
                "@RepositoryRestResource(collectionResourceRel = \"" + name + "\", path = \"" + name + "\")\n" +
                "public interface " + name + "Repository extends ReadOnlyPagingAndSortingRepository<" + name + ",String> {\n" +
                "}";
        try {
            File fileDir = new File("D:\\javaClass");
            if(!fileDir.exists()){
                fileDir.mkdir();
            }
            String fileName = "D:/javaClass/" + name + "Repositority.java";
            File file = new File(fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(repositroy.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
            Logger.debug("EndCreaterepository:"+ name + "Repositority.java");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
